package com.szyjjk.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.szyjjk.entity.*;
import com.szyjjk.imageCrawler.CrawlerFlow;
import com.szyjjk.imageCrawler.CrawlerMain;
import com.szyjjk.imageCrawler.CrawlerTool;
import com.szyjjk.service.RunObserve;
import com.szyjjk.service.RunSiteService;
import com.szyjjk.service.SysImageCrawlerService;
import com.szyjjk.util.DateUtil;
import com.szyjjk.util.HttpTool;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ScheduledFuture;

/**
 * @Author yangyue
 * @Description //TODO 爬虫类
 * @Date 14:23 2020/7/21
 * @Param
 * @return
 **/
@Controller
@RequestMapping("/crawler")
public class crawlerController {


    @Autowired
    private RunSiteService runSiteService;
    @Autowired
    private SysImageCrawlerService sysImageCrawlerService;
    @Autowired
    private RunObserve runObserve;

    private int CRAWLERFLOSE = 0;
    private int IMAGEFLOSE = 0;

    /**
     * @return com.alibaba.fastjson.JSONObject
     * @Author yangyue
     * @Description //TODO 启动爬虫--深圳
     * @Date 14:29 2020/4/22
     * @Param []
     **/
    @RequestMapping(value = "/beginCrawler.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject beginCrawler() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        //首先便利所有
        List<SysWebCrawler> crawlerList = runSiteService.findSysWebCrawlerAll();
        for (SysWebCrawler sysWebCrawler : crawlerList) {
            //然后挨个查看是否需要执行程序
            CrawlerTool.systemout("开始执行" + sysWebCrawler.getCrawlerTitle(), "网站爬虫");
            if (sysWebCrawler.getCrawlerState() == 1) {
                long timecha = HttpTool.typeToLong(sysWebCrawler.getCarwlerType());
                long xiangjian = System.currentTimeMillis() - sysWebCrawler.getCrawlerTime().getTime();
                if (HttpTool.isYestoday(sysWebCrawler.getCrawlerTime())) {
                    //需要判断上次时间是不是昨天，如果是，全部归零
                    sysWebCrawler.setCarwlerCount(0);
                    sysWebCrawler.setTodayNumber(0);
                    this.runSiteService.editSysWebCrawler(sysWebCrawler);
                }
                if (xiangjian > timecha) {
                    Calendar date = Calendar.getInstance();
                    date.setTime(new Date());
                    String year = String.valueOf(date.get(Calendar.YEAR));
                    //超过规定时间，立即执行
                    switch (sysWebCrawler.getCrawlerTitle()) {
                        case "香港天文台":
                            this.RunSiteHongkongObsCrawler(year);
                            break;
                        case "潮汐数据":
                            this.RunTideCrawler(year);
                            break;
                        case "太阳辐射":
                            this.RunSiteSunPicCrawler();
                            break;
                        case "水质数据":
                            this.beginWater();
                            break;
                        case "深圳气象数据网":
                            this.RunSiteCrawler();
                            break;
                    }
                }
            }
        }


        return null;
    }


    /**
     * @return com.alibaba.fastjson.JSONObject
     * @Author yangyue
     * @Description //TODO  爬深圳气象局数据  爬了半天，发现查询的时候再去爬更方便
     * @Date 16:36 2020/3/24
     * @Param []
     **/
    @RequestMapping(value = "/RunSiteCrawler.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject RunSiteCrawler() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "深圳气象局数据ok");
        // 开始爬虫程序 便利深圳气象局站点数据 然后查询近七天数据
        List<RunSite> rsult = runSiteService.findSiteByType("深圳气象数据网");
        int counts = 0;
        for (RunSite site : rsult) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DATE, -1);
            //第一种  实时查询数据 但可能存在一些数据没有的情况
            int count1 = RunSiteJson(site, new Date());
            int cout2 = RunSiteJsonTwo(site);
            //第二站  通过多站查询的方式进行查询
            System.out.println("第一种方法更新" + count1 + ";" + "第二种方法更新" + cout2);
            counts = counts + count1 + cout2;
        }
        this.updateSysCralwer("深圳气象数据网", counts);
        return jsonObject;
    }


    public int RunSiteJsonTwo(RunSite site) {
        int count = 0;
        double randomNumber = Math.random();
        String url = "http://data.121.com.cn/wdn/data/download/dimianguance/quyuzhan7day/" + site.getSiteRemark() + ".js?random=" + randomNumber;
        String jsonString = "";
        try {
            jsonString = HttpTool.doGetOnther(url, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSON.parseObject(jsonString.substring(36, jsonString.length() - 12));
        JSONObject jsonRain = jsonObject.getJSONObject("rain");
        count = count + getRunsiteDataOfJosn(jsonRain, site.getId(), "降水");
        JSONObject jsonrh = jsonObject.getJSONObject("rh");
        count = count + getRunsiteDataOfJosn(jsonrh, site.getId(), "相对湿度");
        JSONObject jsontemp = jsonObject.getJSONObject("temp");
        count = count + getRunsiteDataOfJosn(jsontemp, site.getId(), "气温");
        JSONObject jsonwind = jsonObject.getJSONObject("wind");
        count = count + getRunsiteDataOfJosn(jsonwind, site.getId(), "风向");
        JSONObject jsonopessure = jsonObject.getJSONObject("pressure");
        count = count + getRunsiteDataOfJosn(jsonopessure, site.getId(), "气压");
        return count;
    }


    public int getRunsiteDataOfJosn(JSONObject jsonobj, String siteID, String type) {
        int count = 0;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        int i = 0;
        for (String str : jsonobj.keySet()) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            String numberDay = str.replace("Day_", "");//去掉day_ 留下数字
            //判断日期
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            int urlDay = Integer.parseInt(numberDay);
            if (day < urlDay) {
                //小于的话就说明是上个月的事
                calendar.add(Calendar.MONTH, -1);
            } else {
                //大于就说明是这个月的事
            }
            //设置日期
            calendar.set(Calendar.DAY_OF_MONTH, urlDay);
            Date nowDay = calendar.getTime();
            System.out.println("存入的时间" + simpleDateFormat.format(nowDay));
            //这里应该加上一个怕重复的判断
            List<RunSiteData> runSiteDataList = this.runSiteService.findSiteDataByTimeAndType(siteID, nowDay, type);
            System.out.println("时间:" + str + "；" + simpleDateFormat.format(nowDay));
            JSONObject data = JSON.parseObject(String.valueOf(jsonobj.get(str)));
            Map<String, String> eneityDo = new HashedMap();
            for (String strdata : data.keySet()) {
                String number = strdata.split("_")[1];
                if ("0".equals(number.substring(0, 1))) {
                    number = number.substring(1, 2);
                }
                eneityDo.put("dataH" + number, data.getString(strdata));
            }
            RunSiteData siteData = JSON.parseObject(JSON.toJSONString(eneityDo), RunSiteData.class);
            //存入时间
            siteData.setDataTime(nowDay);
            siteData.setId(HttpTool.getRandomChar(32));
            i++;
            siteData.setDataType(type);
            siteData.setSiteId(siteID);
            if (runSiteDataList.size() == 0) {
                count++;
                runSiteService.saveRunSiteDate(siteData);
            } else {
                RunSiteData runSiteData = runSiteDataList.get(0);
                if (runSiteData.getDataH23() == null) {
                    siteData.setId(runSiteData.getId());
                    CrawlerTool.systemout("有数据就进行修改" + runSiteData.getDataType(), "网站爬虫");
                    count++;
                    this.runSiteService.editRunSiteDate(siteData);
                } else {
                    CrawlerTool.systemout("该数据已经进行进行存储：" + simpleDateFormat.format(nowDay) + ": (" + type + ")", "网站爬虫");
                }
            }

        }
        return count;
    }


    /**
     * @return void
     * @Author yangyue
     * @Description //TODO 第一种方式通过多站查询，优点是可以查到历史数据，确定是有时候一些要素查询不到
     * @Date 11:14 2020/3/25
     * @Param [site]
     **/
    public int RunSiteJson(RunSite site, Date time) {
        int count = 0;
        String[] SZ_TYPE = {"T", "MINR", "U", "WDIDF", "P", "V"};
        String[] SZ_TYPETITLE = {"气温", "降水", "相对湿地", "风速", "气压", "能见度"};
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < SZ_TYPE.length; i++) {
            String ulr = "http://data.121.com.cn/wdn/quyuchanpin/datatrend.do?dataType=0&yaosu=" + SZ_TYPE[i] + "&sdate=" + simpleDateFormat.format(time) + "&obtid=" + site.getSiteNumber() + "&count=1";
            String josntring = "";
            try {
                josntring = HttpTool.doGetOnther(ulr, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
            RunSiteData siteData = getRunsiteDataOfJosn(josntring);
            if (siteData.getDataH0() == null) {
                CrawlerTool.systemout("查询不到数据：" + site.getSiteName() + "的：" + SZ_TYPETITLE[i] + "对应url" + ulr, "网站爬虫");
            }
            siteData.setSiteId(site.getId());

            siteData.setDataType(SZ_TYPETITLE[i]);
            try {
                siteData.setDataTime(simpleDateFormat.parse(simpleDateFormat.format(time)));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            //首先判断是否有这个，如果有那就修改，如果没有就添加
            List<RunSiteData> runSiteDataList = this.runSiteService.findSiteDataByTimeAndType(site.getId(), time, SZ_TYPETITLE[i]);
            if (runSiteDataList.size() == 0) {
                siteData.setId(HttpTool.getRandomChar(32));
                //没有添加
                count++;
                runSiteService.saveRunSiteDate(siteData);
            } else {
                RunSiteData runSiteDataResult = runSiteDataList.get(0);
                siteData.setId(runSiteDataResult.getId());
                //说明23点有数据
                if (runSiteDataResult.getDataH23() == null) {
                    //有就修改
                    count++;
                    runSiteService.editRunSiteDate(siteData);
                } else {
                    CrawlerTool.systemout("数据已满", "网站爬虫");
                }
            }
        }
        return count;
    }

    public static RunSiteData getRunsiteDataOfJosn(String jsonString) {

        JSONObject data = JSON.parseObject(jsonString);
        if (jsonString.length() < 5 || data.getJSONArray("categories") == null || data.getJSONArray("data1") == null || data.getJSONArray("data1").size() == 0) {
            return new RunSiteData();
        }
        JSONArray array = data.getJSONArray("categories");
        JSONArray arrayData = data.getJSONArray("data1");
        Map<String, String> eneityDo = new HashedMap();
        for (int i = 0; i < array.size(); i++) {
            eneityDo.put("dataH" + array.get(i), arrayData.get(i).toString());
        }
        RunSiteData siteData = JSON.parseObject(JSON.toJSONString(eneityDo), RunSiteData.class);
        return siteData;
    }


    /*****下面是  尖鼻咀 潮汐数据****/

    /**
     * @return com.alibaba.fastjson.JSONObject
     * @Author yangyue
     * @Description //TODO 爬尖鼻咀潮汐数据  需要做重复判断
     * @Date 10:22 2020/3/31
     * @Param []
     **/
    @RequestMapping(value = "/RunTideCrawler.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject RunTideCrawler(String year) {
        int count = 0;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        if (year == null || ("").equals(year)) {
            jsonObject.put("msg", "你没输入参数");
            return jsonObject;
        }
        List<RunSite> siteList = this.runSiteService.findSiteByType("潮汐数据");
        for (RunSite site : siteList) {
            String downUrl = "https://www.hko.gov.hk/tide/" + site.getSiteNumber() + year + ".html";
            if (runObserve.findYearTide(year, site.getId())) {
                CrawlerTool.systemout(site.getSiteName() + "已处理过了（" + year + ")", "网站爬虫");
//                return jsonObject;
            } else {
                String josntring = "";
                try {
                    josntring = HttpTool.doGetOnther(downUrl, null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                String[] ararySplte = josntring.split("日期");
                for (int i = 1; i < ararySplte.length; i++) {
                    String item_A = ararySplte[i].substring(54);
                    if (item_A.contains("</PRE>")) {
                        item_A = item_A.split("</PRE>")[0];
                    }
                    String[] item_B = item_A.split("\n");
                    //获取数据，item_B[i1] 为tr td 类型
                    for (int i1 = 2; i1 < item_B.length; i1++) {
                        //trString 为tr 数据
                        RunTide runTide = new RunTide();
                        String trString = item_B[i1];
                        if (trString.contains("<TD>")) {
                            String[] tdArray = trString.split("<TD>");
                            //首先截取出日期
                            String month = tdArray[1].substring(0, 2);
                            String day = tdArray[2].substring(0, 2);
                            Calendar calendar = Calendar.getInstance();
                            calendar.set(Calendar.YEAR, Integer.parseInt(year));
                            calendar.set(Calendar.MONTH, Integer.parseInt(month) - 1);
                            calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day));
                            calendar.set(Calendar.HOUR_OF_DAY, 0);
                            calendar.set(Calendar.MINUTE, 0);
                            calendar.set(Calendar.SECOND, 0);

                            //当前日期
                            Date nowDay = calendar.getTime();
                            //height1
                            if (tdArray.length > 3) {
                                Date hightTime = DateUtil.getHeightTime(tdArray[3], nowDay);
                                if (hightTime != null) {
                                    runTide.setTideTime1(hightTime);
                                    runTide.setTideHight1(tdArray[4].replaceAll("</TD>", ""));
                                }
                            }

                            //highet2
                            if (tdArray.length > 5) {
                                Date hightTime = DateUtil.getHeightTime(tdArray[5], nowDay);
                                if (hightTime != null) {
                                    runTide.setTideTime2(hightTime);
                                    runTide.setTideHight2(tdArray[6].replaceAll("</TD>", ""));
                                }
                            }
                            //highet3
                            if (tdArray.length > 7) {
                                Date hightTime = DateUtil.getHeightTime(tdArray[7], nowDay);
                                if (hightTime != null) {
                                    runTide.setTideTime3(hightTime);
                                    runTide.setTideHight3(tdArray[8].replaceAll("</TD>", ""));
                                }
                            }
                            //highet4
                            if (tdArray.length > 9) {
                                Date hightTime = DateUtil.getHeightTime(tdArray[9], nowDay);
                                if (hightTime != null) {
                                    runTide.setTideTime4(hightTime);
                                    runTide.setTideHight4(tdArray[10].replaceAll("</TD></TR>", ""));
                                }
                            }
                            runTide.setTideDate(nowDay);
                            runTide.setSideId(site.getId());
                            runTide.setId(HttpTool.getRandomChar(32));
                            count++;
                            runObserve.saveRunTide(runTide);
                        }
                    }
                }
            }
        }
        jsonObject.put("msg", "潮汐数据ok");
        this.updateSysCralwer("潮汐数据", count);
        return jsonObject;
    }


    //查询并保存Site By 香港天文台  气象数据  用一次就够了！！！！！！
    @RequestMapping(value = "/RunSiteCrawlerByHongkong.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject RunSiteCrawlerByHongkong() {
        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("code", 0);
//        String downUrl = "https://www.hko.gov.hk/cis/aws/awsInfo_uc.xml";
//        String jsonString = "";
//        try {
//            jsonString = HttpTool.doGetOnther(downUrl, null);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        JSONObject resultObject = JSON.parseObject(jsonString);
//        JSONArray resultArray = resultObject.getJSONArray("aws");
//        for (int i = 0; i < resultArray.size(); i++) {
//            RunSite site = new RunSite();
//            JSONObject partDaily = resultArray.getJSONObject(i);
//            String codeString = partDaily.getString("code");
//            String engString = partDaily.getString("eng");
//            String chiMonthString = partDaily.getString("chi");
//            site.setId(HttpTool.getRandomChar(32));
//            site.setSiteName(chiMonthString);
//            site.setSiteNumber(codeString);
//            site.setSiteType("香港天文台");
//            site.setSiteRemark(engString);
////            runSiteService.saveRunSite(site);
//        }
//        jsonObject.put("msg", "香港天文台站点ok");
        return jsonObject;
    }


    @RequestMapping(value = "/RunSiteHongkongObsCrawler.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject RunSiteHongkongObsCrawler(String year) {
        int count = 0;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        if (year == null || "".equals(year)) {
            jsonObject.put("msg", "你没输入参数");
            return jsonObject;
        }
        List<RunSite> Sitersult = runSiteService.findSiteByType("香港天文台");
        for (RunSite site : Sitersult) {
            String url = "https://www.hko.gov.hk/cis/aws/dailyExtract/dailyExtract_" + site.getSiteNumber() + "_" + year + ".xml";
            System.out.println(url);
            String jsonString = "";
            JSONObject resultObject = new JSONObject();
            try {
                jsonString = HttpTool.doGetOnther(url, null);
                if (jsonString.contains("對不起，我們找不到你要的網頁")) {
                    System.out.println("暂未找到该站点数据");
                    continue;
                }
                resultObject = JSON.parseObject(jsonString);
            } catch (Exception e) {
                System.out.println(e);
            }
            JSONObject stnObject = resultObject.getJSONObject("stn");
            JSONArray dataObject = stnObject.getJSONArray("data");
            for (int i = 0; i < dataObject.size(); i++) {
                JSONObject partDaily = dataObject.getJSONObject(i);
                String month = partDaily.getString("month");          //月份
                JSONArray dayDataArray = partDaily.getJSONArray("dayData");
                for (int dayI = 0; dayI < dayDataArray.size(); dayI++) {
                    JSONArray valueArray = dayDataArray.getJSONArray(dayI);
                    System.out.println(valueArray.size());
                    RunSiteHKObs runSiteHKObs = new RunSiteHKObs();
                    Calendar calendar = Calendar.getInstance();
                    int yearI = Integer.parseInt(year);
                    int mothjI = Integer.parseInt(month);
                    int day = Integer.parseInt((String) valueArray.get(0));
                    calendar.set(yearI, mothjI - 1, day); //月份-1
                    int rusltList = runSiteService.findSiteHongkongByDateAndSiteID(site.getId(), calendar.getTime());
                    if (rusltList == 0) {
                        runSiteHKObs.setObsTime(calendar.getTime());
                        runSiteHKObs.setObsQiya((String) valueArray.get(1));        //气压
                        runSiteHKObs.setObsQiwenMax((String) valueArray.get(2));    //气温 最高
                        runSiteHKObs.setObsQiwenPj((String) valueArray.get(3));     //气温 平均
                        runSiteHKObs.setObsQiwenMin((String) valueArray.get(4));    //气温 最低
                        runSiteHKObs.setObsLudian((String) valueArray.get(5));      //露点
                        runSiteHKObs.setObsShidu((String) valueArray.get(6));       //湿度
                        if (valueArray.size() == 10) {
                            //10 是不包含云量
                            runSiteHKObs.setObsYuliang((String) valueArray.get(7));
                            runSiteHKObs.setObsFengxiang((String) valueArray.get(8));
                            runSiteHKObs.setObsFengsu((String) valueArray.get(9));
                        } else if (valueArray.size() == 11) {
                            //11 包含云量
                            runSiteHKObs.setObsYunliang((String) valueArray.get(7));
                            runSiteHKObs.setObsYuliang((String) valueArray.get(8));
                            runSiteHKObs.setObsFengxiang((String) valueArray.get(9));
                            runSiteHKObs.setObsFengsu((String) valueArray.get(10));
                        }
                        runSiteHKObs.setId(HttpTool.getRandomChar(32));
                        runSiteHKObs.setSiteId(site.getId());
                        count++;
                        this.runSiteService.saveRunSiteHongKongObs(runSiteHKObs);
                    }
                }
            }
        }
        jsonObject.put("msg", "香港天文台数据ok");
        this.updateSysCralwer("香港天文台", count);
        return jsonObject;
    }


    //获得太阳辐射的图片
    @RequestMapping(value = "/RunSiteSunPicCrawler.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject RunSiteSunPicCrawler() {
        SysWebCrawler sysWebCrawler = this.runSiteService.getSycCralwerByTitle("太阳辐射");
        String downPath = sysWebCrawler.getCarwlerRemark();
        File f = new File(downPath);
        if (!f.exists()) {
            f.mkdirs();
        }
        int count = 0;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        List<RunSite> arraySun = runSiteService.findSiteByType("太阳辐射");
        for (RunSite site : arraySun) {
            String[] downUrl = site.getSiteRemark().split(";");
//            String downtitle[] = site.getSiteNumber().split(";");
            int runSunList = this.runObserve.findSunByTimeAndSiteID(HttpTool.simpToData(new Date()), site.getId());
            if (runSunList == 0) {
                //第一个图片下载
                RunSun runSun1 = new RunSun();
                runSun1.setSunTime(HttpTool.simpToData(new Date()));
                String downPNGPath1 = downPath + HttpTool.getRandomChar(32) + ".png";
                File localFile1 = new File(downPNGPath1);
                HttpTool.downloadImage(downUrl[0], "PNG", localFile1);
                runSun1.setSiteId(site.getId());
                runSun1.setId(HttpTool.getRandomChar(32));
                runSun1.setSunGrossPath(downPNGPath1);  //总量
                //第二个图片下载
                String downPNGPath2 = downPath + HttpTool.getRandomChar(32) + ".png";
                File localFile2 = new File(downPNGPath2);
                HttpTool.downloadImage(downUrl[1], "PNG", localFile2);
                runSun1.setSunDirectPath(downPNGPath2);//直射量
                count++;
                this.runObserve.saveRunSun(runSun1);
            }

        }
        jsonObject.put("msg", "太阳辐射ok");
        this.updateSysCralwer("太阳辐射", count);
        return jsonObject;
    }

//    /**
//     * @return com.alibaba.fastjson.JSONObject
//     * @Author yangyue
//     * @Description //TODO 爬台风数据
//     * @Date 11:04 2020/4/3
//     * @Param []
//     **/
//    @RequestMapping(value = "/beginTyphoon.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
//    @ResponseBody
//    public JSONObject beginTyphoon() {
//        try {
//            CrawlerTyphoon.beginCralwer();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("code", 0);
//        jsonObject.put("msg", "台风ok");
//        return jsonObject;
//    }


    /**
     * @return com.alibaba.fastjson.JSONObject
     * @Author yangyue
     * @Description //TODO 水质
     * @Date 11:52 2020/4/8
     * @Param []
     **/
    @RequestMapping(value = "/beginWater.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject beginWater() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "水质数据ok");
        int count = 0;
        List<RunSite> runSites = this.runSiteService.findSiteByType("水质数据");
        for (RunSite runSite : runSites) {
            System.out.println(runSite.getSiteNumber());
            String httmText = null;
            httmText = HttpTool.waterHTML(runSite.getSiteNumber());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/yyyy");
            String[] spileString = httmText.split("<tr class=\"initial\">");

            for (int i = 1; i < spileString.length; i++) {
                String[] td = spileString[i].trim().split("<td>");
//                String asdfa=HttpTool.StringTrim(td[2]);
                Date date = null;
                try {
                    date = simpleDateFormat.parse(HttpTool.StringTrim(td[2]));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                int list = this.runObserve.findWaterBySiteIDAndDate(date, runSite.getId());
                if (list == 0) {
                    RunWater runWater = new RunWater();
                    runWater.setId(HttpTool.getRandomChar(32));
                    runWater.setSiteId(runSite.getId());
                    runWater.setWaterTime(date);
                    runWater.setWaterOxygen(HttpTool.StringTrim(td[3]));
//                    System.out.println("溶解氧 (毫克/升)："+HttpTool.StringTrim(td[3]));
                    String value4 = HttpTool.StringTrim(td[4]);
                    value4 = value4.substring(0, value4.indexOf("</tr>"));
                    if (value4.contains("&lt;")) {
                        value4 = "<1";
                    }
                    runWater.setWaterGerm(value4);
                    count++;
                    this.runObserve.saveRunWater(runWater);
                }

            }
        }
        this.updateSysCralwer("水质数据", count);
        return jsonObject;
    }


    /**
     * @return com.alibaba.fastjson.JSONObject
     * @Author yangyue
     * @Description //TODO 爬虫分页
     * @Date 10:40 2020/4/22
     * @Param [pageNo, pageSize]
     **/
    @RequestMapping(value = "/getPageCrawler.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject getPageCrawler(int pageNo) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);

        List<SysWebCrawler> sysWebCrawlerList = runSiteService.findSysWebCrawlerPage((pageNo - 1) * 10, 10);
        jsonObject.put("list", sysWebCrawlerList);
        jsonObject.put("count", runSiteService.findSysWebCrawlerPagecCount());
        return jsonObject;
    }


    @RequestMapping(value = "/getPageImageCrawler.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject getPageImageCrawler(int pageNo) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        List<SysImageCrawler> sysWebCrawlerList = sysImageCrawlerService.findPageCrawler((pageNo - 1) * 10, 10, null);
        jsonObject.put("list", sysWebCrawlerList);
        jsonObject.put("count", sysImageCrawlerService.findCrewlerAll().size());
        return jsonObject;
    }

    /**
     * @return com.alibaba.fastjson.JSONObject
     * @Author yangyue
     * @Description //TODO 启动和暂停任务
     * @Date 9:51 2020/4/23
     * @Param [id, state]
     **/
    @RequestMapping(value = "/activateTask.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject activateTask(String id, int state) {
        JSONObject jsonObject = new JSONObject();
        SysWebCrawler sysWebCrawler = this.runSiteService.getbyiDSysCralwer(id);
        sysWebCrawler.setCrawlerState(state);
        this.runSiteService.editSysWebCrawler(sysWebCrawler);
        jsonObject.put("code", "0");
        return jsonObject;
    }


    //更新爬虫
    public void updateSysCralwer(String type, int counts) {
        SysWebCrawler sysWebCrawler = this.runSiteService.getSycCralwerByTitle(type);
        sysWebCrawler.setCrawlerTime(new Date());
        sysWebCrawler.setCarwlerCount(sysWebCrawler.getCarwlerCount() + counts);
        sysWebCrawler.setTodayNumber(sysWebCrawler.getTodayNumber() + 1);
        this.runSiteService.editSysWebCrawler(sysWebCrawler);
    }

    @Autowired
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;


    public static Map<String, ScheduledFuture<?>> taskmap = Maps.newHashMap();

    /**
     * @return com.alibaba.fastjson.JSONObject
     * @Author yangyue
     * @Description //TODO 开启爬虫
     * @Date 15:15 2020/4/23
     * @Param []
     **/
    @RequestMapping(value = "/CrawlerThead.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String CrawlerThead(HttpServletRequest request, String type) {

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                // run方法具体重写
//                    System.out.println("开始执行网站爬虫");
                beginCrawler();
            }
        });

        return "";

//        if (request.getServletContext().getAttribute("Crawler") == null) {
//            request.getServletContext().setAttribute("Crawler", 0);
//            while (true) {
//                Thread t = new Thread(new Runnable() {
//                    public void run() {
//                        // run方法具体重写
//                        beginCrawler();
//                    }
//                });
//                t.start();
//                try {
//                    Thread.sleep(1 * 60 * 60 * 1000);
////                    t.stop();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//
//            }
//        } else {
//            return "我才不让你再来一次";
//        }
    }

    /**
     * @return int
     * @Author yangyue
     * @Description //TODO 判断只一开启一次
     * @Date 15:20 2020/4/23
     * @Param []
     **/
    @RequestMapping(value = "/CrawlerTheadFlose.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject CrawlerTheadFlose(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        //爬虫
        Object Crawler = request.getServletContext().getAttribute("Crawler");
        jsonObject.put("Crawler", Crawler);
        //影像
        Object Image = request.getServletContext().getAttribute("Image");
        jsonObject.put("Image", Image);
        //硬件
        Object Hardware = request.getServletContext().getAttribute("Hardware");
        jsonObject.put("Hardware", Hardware);
        return jsonObject;
    }


    /**
     * @return com.alibaba.fastjson.JSONObject
     * @Author yangyue
     * @Description //TODO 启动影像下载
     * @Date 16:36 2020/4/24
     * @Param []
     **/
    @RequestMapping(value = "/beginImageCrawler.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject beginImageCrawler(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        if (request.getServletContext().getAttribute("Image") == null) {
            request.getServletContext().setAttribute("Image", 0);
            jsonObject.put("msg", "启动完毕");
            CrawlerMain.ModQury();
        } else {
            jsonObject.put("msg", "已经启动了");
        }

        return jsonObject;
    }


    @RequestMapping(value = "/beginOneCrawler.do", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String beginOneCrawler() {
        List<SysImageCrawler> allCralerList = sysImageCrawlerService.findCrewlerAll();
        for (SysImageCrawler sysImageCrawler : allCralerList) {
            try {
                switch (sysImageCrawler.getSatellite()) {
                    case "Aviso":
                        CrawlerFlow.crawlerAviso(sysImageCrawler);
                        break;
                    case "MOD021KM,MYD021KM":  //绿潮
                        CrawlerFlow.crawlerMOD(sysImageCrawler);
                        break;
                    case "Himiwari":
                        CrawlerFlow.crawlerHimiwari(sysImageCrawler);
                        break;
                    default:
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "启动";
    }


    /**
     * @return boolean
     * @Author yangyue
     * @Description //TODO 修改webcrawler
     * @Date 11:33 2020/5/8
     * @Param [sysWebCrawler]
     **/
    @RequestMapping(value = "/updateCrawler.do", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public boolean updateCrawler(SysWebCrawler sysWebCrawler) {
        // 参数校验
        this.runSiteService.editSysWebCrawler(sysWebCrawler);
        return true;
    }

    /**
     * @return boolean
     * @Author yangyue
     * @Description //TODO 修改 ImageCrawler
     * @Date 11:32 2020/5/8
     * @Param [sysImageCrawler]
     **/
    @RequestMapping(value = "/updateImageCrawler.do", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public boolean updateImageCrawler(SysImageCrawler sysImageCrawler) {
        SysImageCrawler editSysImageCrawler = sysImageCrawlerService.findbyID(sysImageCrawler.getId());
        editSysImageCrawler.setLatLeft(sysImageCrawler.getLatLeft());
        editSysImageCrawler.setLatRight(sysImageCrawler.getLatRight());
        editSysImageCrawler.setLonDown(sysImageCrawler.getLonDown());
        this.sysImageCrawlerService.editCrawler(editSysImageCrawler);
        return true;
    }


    @RequestMapping(value = "/updateImage.do", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public boolean updateImage(@ModelAttribute SysImageCrawler sysImageCrawler) {
        // 参数校验
        this.sysImageCrawlerService.editCrawler(sysImageCrawler);
        return true;
    }


}
